/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ms.dal.dao;

import java.util.List;
import ms.bus.util.Constant;
import ms.dal.dto.Movies;
import ms.dal.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author minh
 */
public class MoviesDao {

    private Session session;

    public MoviesDao() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public Movies getById(int movieId) {
        session.getTransaction().begin();
        Movies movie = (Movies) session.get(Movies.class, movieId);
        session.flush();
        session.getTransaction().commit();
        return movie;
    }

    public List getAll() {
        session.getTransaction().begin();
        List<Movies> movieList = session.createQuery(Constant.MOVIES_QUERY_SELECT_ALL).list();
        session.flush();
        session.getTransaction().commit();
        return movieList;
    }

    public void insert(Movies movie) {
        try {
            session.getTransaction().begin();
            session.save(movie);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }
    
    public void update(Movies movie) {
        try {
            session.getTransaction().begin();
            session.update(movie);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }
}
